package com.retailshop.shopfinace.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.retailshop.shopfinace.common.InterfaceVer;
import com.retailshop.shopfinace.common.MessageConstants;
import com.retailshop.shopfinace.common.Response;
import com.retailshop.shopfinace.entity.*;
import com.retailshop.shopfinace.mapper.FinaceMapper;
import com.retailshop.shopfinace.service.FinaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*@Transactional*/
@Service("FinaceService")
public class FinaceServiceImpl implements FinaceService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    FinaceMapper finaceMapper;

    @Override
    public List<Finace> selectFinaceList(Finace record) {
        return finaceMapper.selectFinaceList(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer id, Integer classify) {
        return finaceMapper.deleteByPrimaryKey(id, classify);
    }

    @Override
    public int insertFinace(Finace record) {
        return finaceMapper.insertFinace(record);
    }

    @Override
    public int insertFinaceOrder(FinaceOrder record) {
        return finaceMapper.insertFinaceOrder(record);
    }

    @Override
    public FinaceBalance SelectBalance(FinaceBalance record) {

        return finaceMapper.SelectBalance(record);
    }

    @Override
    public FinaceBalanceAll SelectBalanceAll(FinaceBalanceAll record) {

        return finaceMapper.SelectBalanceAll(record);
    }

    @Override
    public FinaceBalanceAllX SelectBalanceAllX(FinaceBalanceAllX record) {
        return finaceMapper.SelectBalanceAllX(record);
    }

    @Override
    public FinaceBalanceAllJ SelectBalanceAllJ(FinaceBalanceAllJ record) {
        return finaceMapper.SelectBalanceAllJ(record);
    }

    @Override
    public List<Finace> selectWeChatList(Integer userNoId, String remark) {
        return finaceMapper.selectWeChatList(userNoId, remark);
    }

    @Override
    public List<Finace> selectWeChatListType(Integer classify, Integer userNoId, String remark) {
        return finaceMapper.selectWeChatListType(classify, userNoId, remark);
    }

    @Override
    public int updateByPrimaryKeySelective(Finace record) {
        return finaceMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Finace SelectDisId() {
        return finaceMapper.SelectDisId();
    }

    @Transactional
    @Override
    public Response refund(List<Finace> finaceList1, List<Finace> finaceList2, List<Finace> finaceList3, List<Finace> finaceList4, Order order) {
        Response response4 = refundAmount4(finaceList4, order);
        if (!response4.getCode().equals("000000")) {
            return response4;
        } else {
            Response response1 = refundAmount1(finaceList1, order);
            if (!response1.getCode().equals("000000")) {
                return response1;
            } else {
                Response response2 = refundAmount2(finaceList2, order);
                if (!response2.getCode().equals("000000")) {
                    return response2;
                } else {
                    Response response3 = refundAmount3(finaceList3, order);
                    if (!response3.getCode().equals("000000")) {
                        return response3;
                    }
                }
            }
        }
        return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款成功", null);
    }

    @Transactional
    @Override
    public Response refundAmount1(List<Finace> finaceList, Order order) {
        LettuceConnectionFactory lettuceConnectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
        if(lettuceConnectionFactory != null){
            lettuceConnectionFactory.setDatabase(0);
            redisTemplate.setConnectionFactory(lettuceConnectionFactory);
            lettuceConnectionFactory.resetConnection();
        }

        double inter = order.getInter();
        double costInter = order.getCostInter();
        double cashCoupon = order.getCashCoupon();
        double coupon = order.getCoupon();
        int userNoId = order.getUsernoId();
        int usernoid = -1;
        String orderNo = order.getOrderNo();
        if (finaceList != null && finaceList.size() > 0) {
            //整体判断
            List<Finace> finaces = finaceMapper.selectBalanceYuE(1, orderNo);
            for (Finace finace : finaces) {
                double amountALL = finace.getAmount() == null ? 0 : finace.getAmount();
                int usernoidALL = finace.getUserId() == null ? 0 : finace.getUserId();
                if (amountALL > 0) {
                    FinaceBalanceAll finaceBalanceAllSelect = new FinaceBalanceAll();
                    finaceBalanceAllSelect.setUserId(usernoidALL);
                    FinaceBalanceAll finaceBalanceAll = finaceMapper.SelectBalanceAll(finaceBalanceAllSelect);
                    double amountT = finaceBalanceAll.getBalance1() == null ? 0 : finaceBalanceAll.getBalance1();
                    if (amountT < amountALL) {
                        Object oneLimit=redisTemplate.opsForValue().get("ID"+usernoidALL);
                        String userNo= InterfaceVer.VerStr((String) oneLimit);
                        RedisUserNo redisUserNo = JSON.parseObject(userNo, new TypeReference<RedisUserNo>() {});
                        String userNoStr="";
                        if(redisUserNo!=null){
                            userNoStr=redisUserNo.getNo();
                        }
                        return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "ALL：积分余额不足" + usernoidALL+"-"+userNoStr, null);
                    }
                }
            }
            //整体判断
            for (Finace finace : finaceList) {
                Boolean state = finace.getState() == null ? false : finace.getState();
                double amount = finace.getAmount() == null ? 0 : finace.getAmount();
                Finace finace1 = finace;
                usernoid = finace1.getUserId() == null ? 0 : finace1.getUserId();
                String remark = "退款" + finace1.getRemark();
                if (state) {
                    if (amount > 0) {
                        FinaceBalanceAll finaceBalanceAllSelect = new FinaceBalanceAll();
                        finaceBalanceAllSelect.setUserId(usernoid);
                        FinaceBalanceAll finaceBalanceAll = finaceMapper.SelectBalanceAll(finaceBalanceAllSelect);
                        double amountT = finaceBalanceAll.getBalance1() == null ? 0 : finaceBalanceAll.getBalance1();
                        if (amountT < amount) {
                            Object oneLimit=redisTemplate.opsForValue().get("ID"+usernoid);
                            String userNo= InterfaceVer.VerStr((String) oneLimit);
                            RedisUserNo redisUserNo = JSON.parseObject(userNo, new TypeReference<RedisUserNo>() {});
                            String userNoStr="";
                            if(redisUserNo!=null){
                                userNoStr=redisUserNo.getNo();
                            }
                            return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "积分余额不足" + usernoid+"-"+userNoStr, null);
                        }
                        Finace finace2 = new Finace();
                        finace2.setState(true);
                        finace2.setClassify(1);
                        finace2.setDel(false);
                        finace2.setUserId(usernoid);
                        finace2.setUID(usernoid);
                        finace2.setAmount(-amount);
                        finace2.setRemark(remark);
                        int res1 = finaceMapper.insertFinace(finace2);
                        if (res1 <= 0) {
                            throw new RuntimeException();
                        }
                    }
                } else {
                    finace1.setState(true);
                    finace1.setClassify(1);
                    int res = finaceMapper.updateByPrimaryKeySelective(finace1);
                    if (res > 0) {
                        if (amount > 0) {
                            Finace finace2 = new Finace();
                            finace2.setState(true);
                            finace2.setClassify(1);
                            finace2.setDel(false);
                            finace2.setUserId(usernoid);
                            finace2.setUID(usernoid);
                            finace2.setAmount(-amount);
                            finace2.setRemark(remark);
                            int res1 = finaceMapper.insertFinace(finace2);
                            if (res1 <= 0) {
                                throw new RuntimeException();
                            }
                        }
                    } else {
                        throw new RuntimeException();
                    }
                }
            }
            if (inter > 0) {
                Finace finace2 = new Finace();
                finace2.setState(true);
                finace2.setClassify(1);
                finace2.setDel(false);
                finace2.setUserId(userNoId);
                finace2.setUID(userNoId);
                finace2.setAmount(inter);
                finace2.setRemark("退款" + orderNo);
                int res1 = finaceMapper.insertFinace(finace2);
                if (res1 <= 0) {
                    throw new RuntimeException();
                }
            }
            return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款成功", null);
        } else {
            if (inter > 0) {
                Finace finace2 = new Finace();
                finace2.setState(true);
                finace2.setClassify(1);
                finace2.setDel(false);
                finace2.setUserId(userNoId);
                finace2.setUID(userNoId);
                finace2.setAmount(inter);
                finace2.setRemark("退款" + orderNo);
                int res1 = finaceMapper.insertFinace(finace2);
                if (res1 <= 0) {
                    throw new RuntimeException();
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款成功", null);
                }
            }
            return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款失败", null);
        }
    }

    @Transactional
    @Override
    public Response refundAmount2(List<Finace> finaceList, Order order) {
        double inter = order.getInter();
        double costInter = order.getCostInter();
        double cashCoupon = order.getCashCoupon();
        double coupon = order.getCoupon();
        int userNoId = order.getUsernoId();
        String orderNo = order.getOrderNo();
        if (finaceList != null && finaceList.size() > 0) {
            //整体判断
            List<Finace> finaces = finaceMapper.selectBalanceYuE(2, orderNo);
            for (Finace finace : finaces) {
                double amountALL = finace.getAmount() == null ? 0 : finace.getAmount();
                int usernoidALL = finace.getUserId() == null ? 0 : finace.getUserId();
                if (amountALL > 0) {
                    FinaceBalanceAll finaceBalanceAllSelect = new FinaceBalanceAll();
                    finaceBalanceAllSelect.setUserId(usernoidALL);
                    FinaceBalanceAll finaceBalanceAll = finaceMapper.SelectBalanceAll(finaceBalanceAllSelect);
                    double amountT = finaceBalanceAll.getBalance2() == null ? 0 : finaceBalanceAll.getBalance2();
                    if (amountT < amountALL) {
                        return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "ALL：购物券余额不足" + usernoidALL, null);
                    }
                }
            }
            //整体判断
            for (Finace finace : finaceList) {
                Boolean state = finace.getState() == null ? false : finace.getState();
                double amount = finace.getAmount() == null ? 0 : finace.getAmount();
                Finace finace1 = finace;
                int usernoid = finace1.getUserId() == null ? 0 : finace1.getUserId();
                String remark = "退款" + finace1.getRemark();
                if (state) {
                    if (amount > 0) {
                        FinaceBalanceAll finaceBalanceAllSelect = new FinaceBalanceAll();
                        finaceBalanceAllSelect.setUserId(usernoid);
                        FinaceBalanceAll finaceBalanceAll = finaceMapper.SelectBalanceAll(finaceBalanceAllSelect);
                        double amountT = finaceBalanceAll.getBalance2() == null ? 0 : finaceBalanceAll.getBalance2();
                        if (amountT < amount) {
                            return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "购物券余额不足" + usernoid, null);
                        }

                        Finace finace2 = new Finace();
                        finace2.setState(true);
                        finace2.setClassify(2);
                        finace2.setDel(false);
                        finace2.setUserId(order.getUsernoId());
                        finace2.setUID(order.getUsernoId());
                        finace2.setAmount(-amount);
                        finace2.setRemark(remark);
                        int res1 = finaceMapper.insertFinace(finace2);
                        if (res1 <= 0) {
                            throw new RuntimeException();
                        }
                    } else {
                        return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "退款失败2", null);
                    }
                } else {
                    finace1.setState(true);
                    finace1.setClassify(2);
                    int res = finaceMapper.updateByPrimaryKeySelective(finace1);
                    if (res > 0) {
                        if (amount > 0) {
                            Finace finace2 = new Finace();
                            finace2.setState(true);
                            finace2.setClassify(2);
                            finace2.setDel(false);
                            finace2.setUserId(order.getUsernoId());
                            finace2.setUID(order.getUsernoId());
                            finace2.setAmount(-amount);
                            finace2.setRemark(remark);
                            int res1 = finaceMapper.insertFinace(finace2);
                            if (res1 <= 0) {
                                throw new RuntimeException();
                            }
                        } else {
                            return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "退款失败1", null);
                        }
                    } else {
                        throw new RuntimeException();
                    }
                }
            }
            if (coupon > 0) {
                Finace finace2 = new Finace();
                finace2.setState(true);
                finace2.setClassify(2);
                finace2.setDel(false);
                finace2.setUserId(userNoId);
                finace2.setUID(userNoId);
                finace2.setAmount(coupon);
                finace2.setRemark("退款" + orderNo);
                int res1 = finaceMapper.insertFinace(finace2);
                if (res1 <= 0) {
                    throw new RuntimeException();
                }
            }
            return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款成功", null);
        } else {
            if (coupon > 0) {
                Finace finace2 = new Finace();
                finace2.setState(true);
                finace2.setClassify(2);
                finace2.setDel(false);
                finace2.setUserId(userNoId);
                finace2.setUID(userNoId);
                finace2.setAmount(coupon);
                finace2.setRemark("退款" + orderNo);
                int res1 = finaceMapper.insertFinace(finace2);
                if (res1 <= 0) {
                    throw new RuntimeException();
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款成功", null);
                }
            }
            return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款失败", null);
        }
    }

    @Transactional
    @Override
    public Response refundAmount3(List<Finace> finaceList, Order order) {
        double inter = order.getInter();
        double costInter = order.getCostInter();
        double cashCoupon = order.getCashCoupon();
        double coupon = order.getCoupon();
        int userNoId = order.getUsernoId();
        String orderNo = order.getOrderNo();
        if (finaceList != null && finaceList.size() > 0) {
            //整体判断
            List<Finace> finaces = finaceMapper.selectBalanceYuE(3, orderNo);
            for (Finace finace : finaces) {
                double amountALL = finace.getAmount() == null ? 0 : finace.getAmount();
                int usernoidALL = finace.getUserId() == null ? 0 : finace.getUserId();
                if (amountALL > 0) {
                    FinaceBalanceAll finaceBalanceAllSelect = new FinaceBalanceAll();
                    finaceBalanceAllSelect.setUserId(usernoidALL);
                    FinaceBalanceAll finaceBalanceAll = finaceMapper.SelectBalanceAll(finaceBalanceAllSelect);
                    double amountT = finaceBalanceAll.getBalance3() == null ? 0 : finaceBalanceAll.getBalance3();
                    if (amountT < amountALL) {
                        return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "ALL：代金券余额不足" + usernoidALL, null);
                    }
                }
            }
            //整体判断

            for (Finace finace : finaceList) {
                Boolean state = finace.getState() == null ? false : finace.getState();
                double amount = finace.getAmount() == null ? 0 : finace.getAmount();
                Finace finace1 = finace;
                int usernoid = finace1.getUserId() == null ? 0 : finace1.getUserId();
                String remark = "退款" + finace1.getRemark();
                if (state) {
                    if (amount > 0) {
                        FinaceBalanceAll finaceBalanceAllSelect = new FinaceBalanceAll();
                        finaceBalanceAllSelect.setUserId(usernoid);
                        FinaceBalanceAll finaceBalanceAll = finaceMapper.SelectBalanceAll(finaceBalanceAllSelect);
                        double amountT = finaceBalanceAll.getBalance3() == null ? 0 : finaceBalanceAll.getBalance3();
                        if (amountT < amount) {
                            return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "代金券余额不足" + usernoid, null);
                        }

                        Finace finace2 = new Finace();
                        finace2.setState(true);
                        finace2.setClassify(3);
                        finace2.setDel(false);
                        finace2.setUserId(order.getUsernoId());
                        finace2.setUID(order.getUsernoId());
                        finace2.setAmount(-amount);
                        finace2.setRemark(remark);
                        int res1 = finaceMapper.insertFinace(finace2);
                        if (res1 <= 0) {
                            throw new RuntimeException();
                        }
                    } else {
                        return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "退款失败2", null);
                    }
                } else {
                    finace1.setState(true);
                    finace1.setClassify(3);
                    int res = finaceMapper.updateByPrimaryKeySelective(finace1);
                    if (res > 0) {
                        if (amount > 0) {
                            Finace finace2 = new Finace();
                            finace2.setState(true);
                            finace2.setClassify(3);
                            finace2.setDel(false);
                            finace2.setUserId(order.getUsernoId());
                            finace2.setUID(order.getUsernoId());
                            finace2.setAmount(-amount);
                            finace2.setRemark(remark);
                            int res1 = finaceMapper.insertFinace(finace2);
                            if (res1 <= 0) {
                                throw new RuntimeException();
                            }
                        } else {
                            return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "退款失败1", null);
                        }
                    } else {
                        throw new RuntimeException();
                    }
                }
            }
            if (cashCoupon > 0) {
                Finace finace2 = new Finace();
                finace2.setState(true);
                finace2.setClassify(3);
                finace2.setDel(false);
                finace2.setUserId(userNoId);
                finace2.setUID(userNoId);
                finace2.setAmount(cashCoupon);
                finace2.setRemark("退款" + orderNo);
                int res1 = finaceMapper.insertFinace(finace2);
                if (res1 <= 0) {
                    throw new RuntimeException();
                }
            }
            return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款成功", null);
        } else {
            if (cashCoupon > 0) {
                Finace finace2 = new Finace();
                finace2.setState(true);
                finace2.setClassify(3);
                finace2.setDel(false);
                finace2.setUserId(userNoId);
                finace2.setUID(userNoId);
                finace2.setAmount(cashCoupon);
                finace2.setRemark("退款" + orderNo);
                int res1 = finaceMapper.insertFinace(finace2);
                if (res1 <= 0) {
                    throw new RuntimeException();
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款成功", null);
                }
            }
            return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款失败", null);
        }
    }

    @Transactional
    @Override
    public Response refundAmount4(List<Finace> finaceList, Order order) {
        LettuceConnectionFactory lettuceConnectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
        if(lettuceConnectionFactory != null){
            lettuceConnectionFactory.setDatabase(0);
            redisTemplate.setConnectionFactory(lettuceConnectionFactory);
            lettuceConnectionFactory.resetConnection();
        }

        double inter = order.getInter();
        double costInter = order.getCostInter();
        double cashCoupon = order.getCashCoupon();
        double coupon = order.getCoupon();
        int userNoId = order.getUsernoId();
        String orderNo = order.getOrderNo();
        if (finaceList != null && finaceList.size() > 0) {
            //整体判断
            List<Finace> finaces = finaceMapper.selectBalanceYuE(4, orderNo);
            for (Finace finace : finaces) {
                double amountALL = finace.getAmount() == null ? 0 : finace.getAmount();
                int usernoidALL = finace.getUserId() == null ? 0 : finace.getUserId();
                if (amountALL > 0) {
                    FinaceBalanceAll finaceBalanceAllSelect = new FinaceBalanceAll();
                    finaceBalanceAllSelect.setUserId(usernoidALL);
                    FinaceBalanceAll finaceBalanceAll = finaceMapper.SelectBalanceAll(finaceBalanceAllSelect);
                    double amountT = finaceBalanceAll.getBalance4() == null ? 0 : finaceBalanceAll.getBalance4();
                    if (amountT < amountALL) {
                        Object oneLimit=redisTemplate.opsForValue().get("ID"+usernoidALL);
                        String userNo= InterfaceVer.VerStr((String) oneLimit);
                        RedisUserNo redisUserNo = JSON.parseObject(userNo, new TypeReference<RedisUserNo>() {});
                        String userNoStr="";
                        if(redisUserNo!=null){
                            userNoStr=redisUserNo.getNo();
                        }
                        return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "ALL：消费积分余额不足" + usernoidALL+"-"+userNoStr, null);
                    }
                }
            }
            //整体判断

            for (Finace finace : finaceList) {
                Boolean state = finace.getState() == null ? false : finace.getState();
                double amount = finace.getAmount() == null ? 0 : finace.getAmount();
                Finace finace1 = finace;
                int usernoid = finace1.getUserId() == null ? 0 : finace1.getUserId();
                String remark = "退款" + finace1.getRemark();
                if (state) {
                    if (amount > 0) {
                        FinaceBalanceAll finaceBalanceAllSelect = new FinaceBalanceAll();
                        finaceBalanceAllSelect.setUserId(usernoid);
                        FinaceBalanceAll finaceBalanceAll = finaceMapper.SelectBalanceAll(finaceBalanceAllSelect);
                        double amountT = finaceBalanceAll.getBalance4() == null ? 0 : finaceBalanceAll.getBalance4();
                        //System.out.println(finaceBalanceAll.toString()+"wwwwwwwwww"+" amountT:"+amountT+" amount:"+amount);
                        if (amountT < amount) {
                            String userNoStr="";
                            Object oneLimit=redisTemplate.opsForValue().get("ID"+usernoid);
                            String userNo= InterfaceVer.VerStr((String) oneLimit);
                            RedisUserNo redisUserNo = JSON.parseObject(userNo, new TypeReference<RedisUserNo>() {});
                            if(redisUserNo!=null){
                                userNoStr=redisUserNo.getNo();
                            }
                            return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "消费积分余额不足" + usernoid+"-"+userNoStr, null);
                        }

                        Finace finace2 = new Finace();
                        finace2.setState(true);
                        finace2.setClassify(4);
                        finace2.setDel(false);
                        finace2.setUserId(order.getUsernoId());
                        finace2.setUID(order.getUsernoId());
                        finace2.setAmount(-amount);
                        finace2.setRemark(remark);
                        int res1 = finaceMapper.insertFinace(finace2);
                        if (res1 <= 0) {
                            throw new RuntimeException();
                        }
                    } else {
                        return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "退款失败2", null);
                    }
                } else {
                    finace1.setState(true);
                    finace1.setClassify(4);
                    int res = finaceMapper.updateByPrimaryKeySelective(finace1);
                    if (res > 0) {
                        if (amount > 0) {
                            Finace finace2 = new Finace();
                            finace2.setState(true);
                            finace2.setClassify(4);
                            finace2.setDel(false);
                            finace2.setUserId(order.getUsernoId());
                            finace2.setUID(order.getUsernoId());
                            finace2.setAmount(-amount);
                            finace2.setRemark(remark);
                            int res1 = finaceMapper.insertFinace(finace2);
                            if (res1 <= 0) {
                                throw new RuntimeException();
                            }
                        } else {
                            return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "退款失败1", null);
                        }
                    } else {
                        throw new RuntimeException();
                    }
                }
            }
            if (costInter > 0) {
                Finace finace2 = new Finace();
                finace2.setState(true);
                finace2.setClassify(4);
                finace2.setDel(false);
                finace2.setUserId(userNoId);
                finace2.setUID(userNoId);
                finace2.setAmount(costInter);
                finace2.setRemark("退款" + orderNo);
                int res1 = finaceMapper.insertFinace(finace2);
                if (res1 <= 0) {
                    throw new RuntimeException();
                }
            }
            return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款成功", null);
        } else {
            if (costInter > 0) {
                Finace finace2 = new Finace();
                finace2.setState(true);
                finace2.setClassify(4);
                finace2.setDel(false);
                finace2.setUserId(userNoId);
                finace2.setUID(userNoId);
                finace2.setAmount(costInter);
                finace2.setRemark("退款" + orderNo);
                int res1 = finaceMapper.insertFinace(finace2);
                if (res1 <= 0) {
                    throw new RuntimeException();
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款成功", null);
                }
            }
            return new Response(MessageConstants.MSG_SUCCESS, null, 0, "退款成功", null);
        }
    }

    @Override
    public int updateByPrimaryKey(Finace record) {
        return finaceMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Finace> selectBalanceYuE(Integer classify, String remark) {
        return finaceMapper.selectBalanceYuE(classify, remark);
    }

    @Override
    public int Add(Map<String, String> map, HttpServletRequest request) {
        int userNoId = 0;//用户编号ID
        int classify = 0;//积分种类
        double amount = 0;//积分金额
        String remark = map.get("remark");//积分备注
        if (map != null) {
            userNoId = map.get("userNoId") == null ? 0 : Integer.parseInt(map.get("userNoId"));
            classify = map.get("classify") == null ? 0 : Integer.parseInt(map.get("classify"));
            amount = map.get("amount") == null ? 0 : Double.parseDouble(map.get("amount"));
        }
        if (classify > 0 && userNoId > 0) {
            Finace finace = new Finace();
            finace.setClassify(classify);
            finace.setUserId(userNoId);
            finace.setAmount(amount);
            finace.setUID(userNoId);
            finace.setRemark(remark);
            finace.setState(true);
            return finaceMapper.insertFinace(finace);
        }
        return 0;
    }

    @Override
    public FinaceBalance GetBalance(Map<String, String> map, HttpServletRequest request) {
        int userNoId = 0;//用户编号ID
        int classify = 0;//积分种类
        if (map != null) {
            userNoId = map.get("userNoId") == null ? 0 : Integer.parseInt(map.get("userNoId"));
            classify = map.get("classify") == null ? 0 : Integer.parseInt(map.get("classify"));
        }
        if (userNoId > 0 && classify > 0) {
            FinaceBalance record = new FinaceBalance();
            record.setUserId(userNoId);
            record.setClassify(classify);
            return finaceMapper.SelectBalance(record);
        }
        return null;
    }

    @Override
    public FinaceBalanceAll GetBalanceAll(Map<String, String> map, HttpServletRequest request) {
        int userNoId = 0;//用户编号ID
        if (map != null) {
            userNoId = map.get("userNoId") == null ? 0 : Integer.parseInt(map.get("userNoId"));
        }
        if (userNoId > 0) {
            FinaceBalanceAll record = new FinaceBalanceAll();
            record.setUserId(userNoId);
            return finaceMapper.SelectBalanceAll(record);
        }
        return null;
    }

    @Override
    public FinaceBalanceAllJ GetBalanceAllJ(Map<String, String> map, HttpServletRequest request) {
        int userNoId = 0;//用户编号ID
        if (map != null) {
            userNoId = map.get("userNoId") == null ? 0 : Integer.parseInt(map.get("userNoId"));
        }
        if (userNoId > 0) {
            FinaceBalanceAllJ record = new FinaceBalanceAllJ();
            record.setUserId(userNoId);
            return finaceMapper.SelectBalanceAllJ(record);
        }
        return null;
    }

    @Override
    public FinaceBalanceAllX GetBalanceAllX(Map<String, String> map, HttpServletRequest request) {
        int userNoId = 0;//用户编号ID
        if (map != null) {
            userNoId = map.get("userNoId") == null ? 0 : Integer.parseInt(map.get("userNoId"));
        }
        if (userNoId > 0) {
            FinaceBalanceAllX record = new FinaceBalanceAllX();
            record.setUserId(userNoId);
            return finaceMapper.SelectBalanceAllX(record);
        }
        return null;
    }

    @Override
    public Response SetFinaceOrder(Map<String, String> map, HttpServletRequest request) {
        int userNoId = 0;//用户编号ID
        if (map != null) {
            System.out.println(map.get("userNoId"));
            userNoId = map.get("userNoId") == null ? 0 : Integer.parseInt(map.get("userNoId"));
        }
        if (userNoId > 0) {

            FinaceBalanceAll finaceBalanceAll1 = new FinaceBalanceAll();
            finaceBalanceAll1.setUserId(userNoId);
            FinaceBalanceAll finaceBalanceAll = finaceMapper.SelectBalanceAll(finaceBalanceAll1);
            if (finaceBalanceAll != null) {

                FinaceOrder finaceOrder = new FinaceOrder();

                //读取参数
                finaceOrder.setUserId(userNoId);
                finaceOrder.setAmount1(map.get("amount1") == null ? 0 : Double.parseDouble(map.get("amount1")));
                finaceOrder.setAmount2(map.get("amount2") == null ? 0 : Double.parseDouble(map.get("amount2")));
                finaceOrder.setAmount3(map.get("amount3") == null ? 0 : Double.parseDouble(map.get("amount3")));
                finaceOrder.setAmount4(map.get("amount4") == null ? 0 : Double.parseDouble(map.get("amount4")));
                finaceOrder.setRemark(map.get("orderNo") == null ? "" : "支付" + map.get("orderNo"));
                finaceOrder.setUID(userNoId);
                //finaceOrder.setUserNo(map.get("userNo")==null?"":map.get("userNo"));
                System.out.println("Amount1:" + finaceOrder.getAmount1());
                System.out.println("Balance1:" + finaceBalanceAll.getBalance1());
                System.out.println("Amount2:" + finaceOrder.getAmount2());
                System.out.println("Balance2:" + finaceBalanceAll.getBalance2());
                System.out.println("Amount3:" + finaceOrder.getAmount3());
                System.out.println("Balance3:" + finaceBalanceAll.getBalance3());
                System.out.println("Amount4:" + finaceOrder.getAmount4());
                System.out.println("Balance4:" + finaceBalanceAll.getBalance4());
                if (finaceOrder.getAmount1() > finaceBalanceAll.getBalance1()) {
                    return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "积分余额不足", request);
                }
                if (finaceOrder.getAmount2() > finaceBalanceAll.getBalance2()) {
                    return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "购物券余额不足", request);
                }
                if (finaceOrder.getAmount3() > finaceBalanceAll.getBalance3()) {
                    return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "代金券余额不足", request);
                }
                if (finaceOrder.getAmount4() > finaceBalanceAll.getBalance4()) {
                    return new Response(MessageConstants.MSG_LACK_OF_BALANCE, null, 0, "消费积分余额不足", request);
                }

                try {
                    //订单支付
                    finaceMapper.insertFinaceOrder(finaceOrder);
                    return new Response(MessageConstants.MSG_SUCCESS, finaceOrder, 1, "", request);
                } catch (Exception ex) {
                    return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "订单支付失败", request);
                }

            } else {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "读取数据错误A", request);
            }
        }
        return null;
    }


    @Override
    public Response selectFinaceUserList(Map<String, String> map, HttpServletRequest request) {
        int userNoId = 0;//用户编号ID
        if (map != null) {
            System.out.println(map.get("userNoId"));
            userNoId = map.get("userNoId") == null ? 0 : Integer.parseInt(map.get("userNoId"));
        }
        if (userNoId > 0) {
            Finace finace = new Finace();
            finace.setUserId(userNoId);
            int classify = map.get("classify") == null ? 0 : Integer.parseInt(map.get("classify"));
            finace.setClassify(classify);
            Boolean state = map.get("state") == null ? null : Boolean.valueOf(map.get("state"));
            System.out.println(state);
            if (state != null) {
                finace.setState(state);
            }
            int page = map.get("page") == null ? 1 : Integer.parseInt(map.get("page"));
            int limit = map.get("limit") == null ? 10 : Integer.parseInt(map.get("limit"));
            PageHelper.startPage(page, limit);
            List<Finace> finaceList = finaceMapper.selectFinaceUserList(finace);
            if (finaceList != null) {

                PageInfo<Finace> pageInfo = new PageInfo<>(finaceList);
                int count = (int) pageInfo.getTotal();
                return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), count, "", request);
            } else {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "没有数据", request);
            }
        } else {
            return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "读取数据错误A", request);
        }
    }

    @Override
    public FinaceEarnings getEarnings(Map<String, String> map, HttpServletRequest request) {
        int userNoId = 0;//用户编号ID
        if (map != null) {
            System.out.println(map.get("userNoId"));
            userNoId = map.get("userNoId") == null ? 0 : Integer.parseInt(map.get("userNoId"));
        }
        if (userNoId > 0) {
            int type = 0;
            try {
                type = Integer.parseInt(map.get("type"));
            } catch (Exception ex) {
            }
            if (type > 0) {
                return finaceMapper.SelectEarnings(userNoId, type);
            }
        }
        return null;
    }

    @Override
    public List<Finace> selectEarningsList(Map<String, String> map, HttpServletRequest request) {
        int userNoId = 0;//用户编号ID
        if (map != null) {
            System.out.println(map.get("userNoId"));
            userNoId = map.get("userNoId") == null ? 0 : Integer.parseInt(map.get("userNoId"));
        }
        if (userNoId > 0) {
            int type = 0;
            try {
                type = Integer.parseInt(map.get("type"));
            } catch (Exception ex) {
            }
            int dateType = 0;
            try {
                dateType = Integer.parseInt(map.get("dateType"));
            } catch (Exception ex) {
            }

            int page = map.get("page") == null ? 1 : Integer.parseInt(map.get("page"));
            int limit = map.get("limit") == null ? 10 : Integer.parseInt(map.get("limit"));
            PageHelper.startPage(page, limit);

            List<Finace> finaceList = null;
            if (dateType == 1) {
                finaceList = finaceMapper.selectEarningsListDay(userNoId, type);
            } else if (dateType == 2) {
                finaceList = finaceMapper.selectEarningsListWeeks(userNoId, type);
            } else if (dateType == 3) {
                finaceList = finaceMapper.selectEarningsListMonth(userNoId, type);
            } else if (dateType == 4) {
                finaceList = finaceMapper.selectEarningsListTotal(userNoId, type);
            }
            if (finaceList != null) {
                PageInfo<Finace> pageInfo = new PageInfo<>(finaceList);
                return pageInfo.getList();
            }
        }
        return null;
    }

    @Override
    public int insertTransfer(Map<String, String> map, HttpServletRequest request) {

        System.out.println("A");

        int userNoId = 0;//用户编号ID
        int userNoId1 = 0;//转账用户ID

        //读取用户编号和姓名


        int classify = 0;//积分种类
        double amount = 0;//积分金额
        String remark = "转账编号ID:";//积分备注
        if (map != null) {
            System.out.println("B");
            userNoId = map.get("userNoId") == null ? 0 : Integer.parseInt(map.get("userNoId"));
            userNoId1 = map.get("userNoId1") == null ? 0 : Integer.parseInt(map.get("userNoId1"));
            classify = map.get("classify") == null ? 0 : Integer.parseInt(map.get("classify"));
            amount = map.get("amount") == null ? 0 : Double.parseDouble(map.get("amount"));
        }
        if (classify > 0 && userNoId > 0 && userNoId1 > 0) {
            System.out.println("C");

            FinaceBalance record = new FinaceBalance();
            record.setUserId(userNoId);
            record.setClassify(classify);
            FinaceBalance finaceBalance = finaceMapper.SelectBalance(record);
            System.out.println("H");
            if (finaceBalance != null) {
                System.out.println("I:" + finaceBalance.getBalance());//余额足够
                if (finaceBalance.getBalance() >= amount) {
                    System.out.println("D");
                    Finace finace = new Finace();
                    finace.setClassify(classify);
                    finace.setUserId(userNoId);
                    finace.setAmount(amount);
                    finace.setUID(userNoId);
                    finace.setRemark(remark + ":" + userNoId1);
                    finace.setRemark1(remark + ":" + userNoId);
                    finace.setState(true);
                    finace.setOID(userNoId1);
                    System.out.println("E");
                    return finaceMapper.insertTransfer(finace);
                } else {
                    return -1;
                }
            }
        }
        return 0;
    }
}
